package com.sdy.controller;



import java.util.Date;
import java.util.Map;

import com.jfinal.core.Controller;
import com.sdy.model.Employer;
import com.sdy.model.Soundcode;

public class SoundcodeController extends Controller {
	
	private Soundcode codedao = Soundcode.dao;
	
	public void showCodes(){
		int page = getParaToInt("page");
		int rows = getParaToInt("rows");
		renderJson(codedao.getCodes(page,rows));
	}
	
	public void saveCode(){
		Soundcode code = getModel(Soundcode.class);
		code.set("createDate", new Date());
		Employer emp = (Employer) getSession().getAttribute("emp");
		String author = code.getStr("author") == null?emp.getStr("name"):code.getStr("author");
		code.set("author", author);
		boolean flag = code.save();
		if(flag){
			render("/admin/code.jsp");
		}
	}
	
	public void updateCode(){
		Soundcode code = getModel(Soundcode.class);
		code.set("createDate", new Date());
		Employer emp = (Employer) getSession().getAttribute("emp");
		String author = code.getStr("author") == null?emp.getStr("name"):code.getStr("author");
		code.set("author", author);
		boolean flag = code.update();
		if(flag){
			render("/admin/code.jsp");
		}
	}
	
	public void deleteCode(){
		boolean flag = codedao.deleteById(getPara("id"));
		if(flag){
			renderJson("success", true);
		}
	}
	
	public void rendPage(){
		int page = getParaToInt("page");
		int rows = getParaToInt("rows");
		Map<String,Object> codes = codedao.getCodes(page,rows);
		setAttr("codes", codes);
		render("/index/code.jsp");
	}
	
	public void rendContent(){
		int codeId = getParaToInt("id");
		Soundcode code = codedao.getCodeById(codeId);
		setAttr("code", code);
		render("/index/codeContent.jsp");
	}
}
